﻿
#region Using Statements

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Freeform;

#endregion


namespace Wizardlings
{
    public class Popup : World
    {

        #region Events

        public event PopupEventHandler Updated;
        public event PopupEventHandler CancelPressed;
        public event PopupEventHandler OKPressed;
        public delegate void PopupEventHandler(Popup Popup);

        #endregion


        #region Members

        public String Message1 { get; set; }
        public Entity Msg1 { get; set; }

        public String Message2 { get; set; }
        public Entity Msg2 { get; set; }

        public MenuItem OK, Cancel;

        private Single HorizontalPosition = 33f;
        private Single HorizontalOffset = 33f;

        #endregion


        #region Initialisation

        public Popup(Freeform.Game Game, Boolean ShowCancelButton)
            : base(Game)
        {
            BackgroundColour = new Color(new Vector4(0, 0, 0, 0.9f));

            Message1 = "";
            Msg1 = new Entity(this);
            Msg1.SpatialProperties.Position = new Vector2(50, 8);
            Msg1.Actor = new Actor_Text2D(Msg1, Message1, Game.Content.Load<SpriteFont>("Fonts/Basic"));
            (Msg1.Actor as Actor_Text2D).UseNaturalFontSize = true;
            (Msg1.Actor as Actor_Text2D).NaturalFontScaling = Vector2.One / 4f;
            Msg1.Actor.Depth = 0.25f;
            Msg1.Actor.ColourOverlay = Color.White;

            Message2 = "";
            Msg2 = new Entity(this);
            Msg2.SpatialProperties.Position = new Vector2(50, 35);
            Msg2.Actor = new Actor_Text2D(Msg2, Message2, Game.Content.Load<SpriteFont>("Fonts/Basic"));
            (Msg2.Actor as Actor_Text2D).UseNaturalFontSize = true;
            (Msg2.Actor as Actor_Text2D).NaturalFontScaling = Vector2.One / 4f;
            Msg2.Actor.Depth = 0.25f;
            Msg2.Actor.ColourOverlay = Color.Red;


            if (!ShowCancelButton)
                HorizontalPosition = 50;

            MakeItem(ref OK, "OK");
            OK.ItemClicked += delegate(MenuItem Item)
            {
                if (OKPressed != null) OKPressed(this);
            };

            if (ShowCancelButton)
            {
                MakeItem(ref Cancel, "Cancel");
                Cancel.ItemClicked += delegate(MenuItem Item) 
                {
                    if (CancelPressed != null) CancelPressed(this);
                };
            }
         
            Viewport2D View = new Viewport2D(this);
            View.Camera.SpatialProperties.Size = new Vector2(100, 75);
            View.Depth = 0.1f;
            View.SpatialProperties.Position = Vector2.One / 4f;
            View.SpatialProperties.Size = Vector2.One / 2f;
        }

        #endregion


        public override void Update(GameTime time)
        {
            base.Update(time);

            (Msg1.Actor as Actor_Text2D).Text = Message1;
            (Msg2.Actor as Actor_Text2D).Text = Message2;

            if (Game.Input.KeyState_Current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Enter))
                if (OKPressed != null) OKPressed(this);

            if (Game.Input.KeyState_Current.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.Escape))
                if (CancelPressed != null) CancelPressed(this);

            if (Updated != null) Updated(this);
        }


        private void ItemHighlighted(MenuItem Item)
        {
            Item.TextActor.ColourOverlay = Color.Red;
            Item.TextActor.NaturalFontScaling = Vector2.One / 3f;
        }

        private void ItemLeft(MenuItem Item)
        {
            Item.TextActor.ColourOverlay = Color.White;
            Item.TextActor.NaturalFontScaling = Vector2.One / 4f;
        }

        private void MakeItem(ref MenuItem Item, String Text)
        {
            Item = new MenuItem(this, Text, "Fonts/Basic");
            Item.SpatialProperties.Position = new Vector2(HorizontalPosition, 70);
            Item.TextActor.NaturalFontScaling = Vector2.One / 4f;
            Item.TextActor.ColourOverlay = Color.White;
            Item.ItemHighlighted += ItemHighlighted;
            Item.ItemLeft += ItemLeft;
            HorizontalPosition += HorizontalOffset;
        }
    }
}